package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Sign;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.SignObjectQuery;
import cn.wolfcode.crm.service.ISignService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("sign")
public class SignController {
    @Autowired
    private ISignService signMapper;

    @RequiresPermissions("sign:index")
    @PermissionName("签到信息主页")
    @RequestMapping("index")
    public String index(@ModelAttribute("qo") SignObjectQuery qo) {
        return "sign/list";
    }

    @RequiresPermissions("sign:list")
    @PermissionName("签到信息查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Sign> list(@ModelAttribute("qo") SignObjectQuery qo) {
        PageResult<Sign> list = signMapper.query(qo);
        return list;
    }



    //改变签到状态
    @RequestMapping("changStateIn")
    @ResponseBody
    public JsonResult changStateIn() {
        JsonResult jsonResult = new JsonResult();
        try {
            signMapper.changStateIn();

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("状态失败");
        }
        return jsonResult;
    }


    //获取签到状态
    @RequestMapping("getStateByEmployee")
    @ResponseBody
    public JsonResult getStateByEmployee() {
        JsonResult jsonResult = new JsonResult();
        try {
           int state= signMapper.getStateByEmployee();
            if(state==-1){
                jsonResult.setErrorMsg("未签到");
            }else if(state==1){
                jsonResult.setErrorMsg("未签退");
            }else if(state==0){
                jsonResult.setErrorMsg("签到签退");
            }else if(state==-2){
                jsonResult.setErrorMsg("请先登录");
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("错误");
        }
        return jsonResult;
    }

    //改变签到状态
    @RequestMapping("changStateOut")
    @ResponseBody
    public JsonResult changStateOut() {
        JsonResult jsonResult = new JsonResult();
        try {
            signMapper.changStateOut();

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("打卡失败");
        }
        return jsonResult;
    }
}
